<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="活动主题" prop="title">
      <el-input v-model="dataForm.title" placeholder="活动主题"></el-input>
    </el-form-item>
    <el-form-item label="活动内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="活动内容"></el-input>
    </el-form-item>
    <el-form-item label="化名" prop="avatar">
      <el-input v-model="dataForm.avatar" placeholder="化名"></el-input>
    </el-form-item>
    <el-form-item label="活动条例图片" prop="itemPicUrl">
      <el-input v-model="dataForm.itemPicUrl" placeholder="活动条例图片"></el-input>
    </el-form-item>
    <el-form-item label="子标题" prop="subtitle">
      <el-input v-model="dataForm.subtitle" placeholder="子标题"></el-input>
    </el-form-item>
    <el-form-item label="活动类别" prop="topicCategoryId">
      <el-input v-model="dataForm.topicCategoryId" placeholder="活动类别"></el-input>
    </el-form-item>
    <el-form-item label="活动价格" prop="priceInfo">
      <el-input v-model="dataForm.priceInfo" placeholder="活动价格"></el-input>
    </el-form-item>
    <el-form-item label="" prop="readCount">
      <el-input v-model="dataForm.readCount" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="场景图片链接" prop="scenePicUrl">
      <el-input v-model="dataForm.scenePicUrl" placeholder="场景图片链接"></el-input>
    </el-form-item>
    <el-form-item label="活动模板Id" prop="topicTemplateId">
      <el-input v-model="dataForm.topicTemplateId" placeholder="活动模板Id"></el-input>
    </el-form-item>
    <el-form-item label="活动标签Id" prop="topicTagId">
      <el-input v-model="dataForm.topicTagId" placeholder="活动标签Id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          title: '',
          content: '',
          avatar: '',
          itemPicUrl: '',
          subtitle: '',
          topicCategoryId: '',
          priceInfo: '',
          readCount: '',
          scenePicUrl: '',
          topicTemplateId: '',
          topicTagId: ''
        },
        dataRule: {
          title: [
            { required: true, message: '活动主题不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '活动内容不能为空', trigger: 'blur' }
          ],
          avatar: [
            { required: true, message: '化名不能为空', trigger: 'blur' }
          ],
          itemPicUrl: [
            { required: true, message: '活动条例图片不能为空', trigger: 'blur' }
          ],
          subtitle: [
            { required: true, message: '子标题不能为空', trigger: 'blur' }
          ],
          topicCategoryId: [
            { required: true, message: '活动类别不能为空', trigger: 'blur' }
          ],
          priceInfo: [
            { required: true, message: '活动价格不能为空', trigger: 'blur' }
          ],
          readCount: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          scenePicUrl: [
            { required: true, message: '场景图片链接不能为空', trigger: 'blur' }
          ],
          topicTemplateId: [
            { required: true, message: '活动模板Id不能为空', trigger: 'blur' }
          ],
          topicTagId: [
            { required: true, message: '活动标签Id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/platform/topic/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.topic.title
                this.dataForm.content = data.topic.content
                this.dataForm.avatar = data.topic.avatar
                this.dataForm.itemPicUrl = data.topic.itemPicUrl
                this.dataForm.subtitle = data.topic.subtitle
                this.dataForm.topicCategoryId = data.topic.topicCategoryId
                this.dataForm.priceInfo = data.topic.priceInfo
                this.dataForm.readCount = data.topic.readCount
                this.dataForm.scenePicUrl = data.topic.scenePicUrl
                this.dataForm.topicTemplateId = data.topic.topicTemplateId
                this.dataForm.topicTagId = data.topic.topicTagId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/platform/topic/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'content': this.dataForm.content,
                'avatar': this.dataForm.avatar,
                'itemPicUrl': this.dataForm.itemPicUrl,
                'subtitle': this.dataForm.subtitle,
                'topicCategoryId': this.dataForm.topicCategoryId,
                'priceInfo': this.dataForm.priceInfo,
                'readCount': this.dataForm.readCount,
                'scenePicUrl': this.dataForm.scenePicUrl,
                'topicTemplateId': this.dataForm.topicTemplateId,
                'topicTagId': this.dataForm.topicTagId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
